{% extends 'myadmin/index.html' %}
{% block title %}
<title>后台商品分类</title>
{% endblock %}

{% block con %}

<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
    <div class="widget am-cf">
        <div class="widget-head am-cf">
            <div class="widget-title  am-cf">商品分类</div>


        </div>
        <div class="widget-body  am-fr">

            <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                <div class="am-form-group">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{% url 'myadmin_cate_add' %}" type="button"
                                class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</a>
                            <button type="button" class="am-btn am-btn-default am-btn-secondary"><span
                                    class="am-icon-save"></span> 保存</button>
                            <button type="button" class="am-btn am-btn-default am-btn-warning"><span
                                    class="am-icon-archive"></span> 审核</button>
                            <button type="button" class="am-btn am-btn-default am-btn-danger"><span
                                    class="am-icon-trash-o"></span> 删除</button>
                        </div>
                    </div>
                </div>
            </div>
            <form>    
            <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                <div class="am-form-group tpl-table-list-select">
                    <select data-am-selected="{btnSize: 'sm'}" name="types" style="display: none;">
                        <option value="all" {% if request.GET.types == 'all' %} selected {% endif %}>所有类别</option>
                        <option value="id" {% if request.GET.types == 'id' %} selected {% endif %}>ID</option>
                        <option value="phone" {% if request.GET.types == 'phone' %} selected {% endif %}>手机号</option>
                        <option value="nikename" {% if request.GET.types == 'nikename' %} selected {% endif %}>昵称</option>
                        <option value="email" {% if request.GET.types == 'email' %} selected {% endif %}>邮箱</option>

                    </select>
                </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                    <input type="text" class="am-form-field" name="keywords" value="{{ request.GET.keywords}}">
                    <span class="am-input-group-btn">
                        <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
                    </span>
                </div>
            </div>
            </form>
            <div class="am-u-sm-12">
                <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>分类名</th>
                            <th>所属父级</th>
                            <th>该类下的商品数量</th>
                            <th>Path路径</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for v in catelist %}
                        <tr style="text-align: center;">
                            <td class="am-text-middle">{{v.id}}</td>
                            <td class="am-text-middle cate_name">{{v.sub}} <span cid="{{v.id}}">{{v.name}}</span></td>
                            <td class="am-text-middle">{{v.pname}}</td>
                            <td class="am-text-middle">{{v.goods_set.count|default:'无'}}</td>
                            <td class="am-text-middle">{{v.path}}</td>
                            
                            <td class="am-text-middle">
                                <div class="tpl-table-black-operation">
                                    <a href="">
                                        <i class="am-icon-pencil"></i> 编辑
                                    </a>
                                    <a href="javascript:void(0);" cid="{{v.id}}" class="removeCate tpl-table-black-operation-del">
                                        <i class="am-icon-trash"></i> 删除
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {%endfor %}
                        <!-- more data -->
                    </tbody>
                </table>
            </div>
            <div class="am-u-lg-12 am-cf">

                <div class="am-fr">
                    <ul class="am-pagination tpl-pagination">
                        {% comment %} <li class="am-disabled"><a href="#">«</a></li>
                        <li class="am-active"><a href="#">1</a></li>
                        {% load pagetag %}

                        {% ShowsPage userlist.paginator.num_pages request%} {% endcomment %}
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $('.cate_name').dblclick(function(){
        var span=$(this).find('span')
        // 获取当前分类名字
        var catename=$(this).find('span').text()
        // 创建一个input
        var inp=$('<input type="text" value="'+catename+'" style="color:black;"></input>')
        // 把分类名换成input
        $(this).find('span').html(inp)
        inp.select()

        // 获取当前选择元素的分类 id
        var cid=span.attr('cid')

        // 给input绑定丧失焦点时间，判断是否更新了name，发送ajax
        inp.blur(function(){
            var newname= $(this).val()
            if(newname== catename){
                span.html(catename)
            }else{
                // 需要发送ajax去更新
                $.get('{% url 'myadmin_cate_edit' %}',{'cid':cid,'newname':newname},function(data){
                    // 判断是否更新成功
                    if(data['code']==0){
                        span.html(newname)
                        alert(data['msg'])
                    }
                })
            }
        })

    })

    $('.removeCate').click(function(){
        //  获取当前的元素的值
        var cid= $(this).attr('cid');
        var a=$(this)
        // 发送ajax请求
        $.get('{%url 'myadmin_cate_del'%}',{'cid':cid},function(data){
            if(data['code'] == 0){
                //删除成功
               a.parents('tr').remove()
            }
            alert(data['msg'])
        },'json')
    })
</script>
{% endblock %}